<?php

/**
 * Формы работы со страницами
 */
class PageForm extends Zend_Form {

    /**
     * Форма страницы
     *
     * @param array $options опции, пока тольк id = #ID страницы
     * @return PageForm
     */
    public function Page($options = null) {
        $t = Zend_Registry::get('Zend_Locale');
        $this->setName('page');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $model = new PageModel();
        $template = new Zend_Form_Element_Select('template');
        $array = $model->getAddonsList('template');
        $template->addMultiOptions(array('0' => $t->_('Default')) + $array)
                ->setLabel('Template')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $lang = new Zend_Form_Element_Select('lang');
        $languages = $model->getAddons('language');
        foreach ($languages as $key => $value) {
            $language[$value->title] = $value->name;
        }
        $lang->addMultiOptions(array('' => $t->_('Default')) + $language)
                ->setLabel('Language')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $parent = new Zend_Form_Element_Select('parent');
        $array = $model->getPagesList();

        $multiOptions = array(0 => $t->_('Undefined'));
        if ($array)
            $multiOptions = (array) $multiOptions + (array) $array;
        $parent->addMultiOptions($multiOptions)
                ->setLabel('Parent page')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $visible = new Zend_Form_Element_Select('visible');
        $array = array(
            '0' => $t->_('Visible'),
            '2' => $t->_('Hidden'),
            '4' => $t->_('Closed')
        );

        $visible->addMultiOptions($array)
                ->setLabel('Visibility')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $description = new Zend_Form_Element_Textarea('description');
        $description->setLabel('Description')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $url = new Zend_Form_Element_Text('url');
        $url->setLabel('Link')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $keywords = new Zend_Form_Element_Textarea('keywords');
        $keywords->setLabel('Keywords')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($name, $url, $parent, $template, $lang, $visible, $title, $description, $keywords, $submit));
        $this->addDisplayGroup(array('name', 'url', 'parent', 'template', 'lang', 'visible'), 'settings', array('legend' => $t->_('Page settings')));
        $this->addDisplayGroup(array('title', 'description', 'keywords', 'submit'), 'seo', array('legend' => 'SEO'));

        $this->setDisplayGroupDecorators(array(
            'FormElements',
            'Fieldset'
        ));
        $this->setAction(ADMIN_URL . '/pages/edit/id/' . $options['id']);

        $this->setDefaultTranslator($t);
        return $this;
    }

    public function Link($options = null) {
        $t = Zend_Registry::get('Zend_Locale');
        $this->setName('page');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Title')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $link = new Zend_Form_Element_Text('url');
        $link->setLabel('Link')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired()
        //->setDescription('Описание <b>этого</b> поля!!!')
        //->getDecorator('Description')->setEscape(false)
        ;

        $model = new PageModel();

        $parent = new Zend_Form_Element_Select('parent');
        $array = $model->getPagesList();

        $multiOptions = array(0 => $t->_('Undefined'));
        if ($array)
            $multiOptions = (array) $multiOptions + (array) $array;
        $parent->addMultiOptions($multiOptions)
                ->setLabel('Parent page')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $url = new Zend_Form_Element_Text('link');
        $url->setLabel('URL')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $blank = new Zend_Form_Element_Checkbox('blank');
        $blank->setLabel('Open in new window?')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($name, $link, $url, $parent, $blank, $submit));

        $this->setAction(ADMIN_URL . '/pages/edit/id/' . $options['id'] . '/link/1');

        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма добавление секции
     *
     * @param array $options Опции, пока только id = #ID секции
     * @return PageForm
     */
    public function AddSection($options = null) {
        $this->setName('section');
        $t = Zend_Registry::get('Zend_Locale');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $model = new PageModel();
        $bid = new Zend_Form_Element_Select('bid');
        $bid->addMultiOptions($model->getBlocks($options['id']))
                ->setLabel('Block')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $mid = new Zend_Form_Element_Select('mid');
        $array = $model->getModulesList();
        $mid->addMultiOptions($array)
                ->setLabel('Module')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();
        if (isset($array[2]))
            $mid->setValue(2);

        $visible = new Zend_Form_Element_Select('visible');
        $array = array(
            '0' => $t->_('Visible'),
            '2' => $t->_('Hidden'),
            '4' => $t->_('Closed')
        );

        $visible->addMultiOptions($array)
                ->setLabel('Visibility')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($name, $mid, $bid, $visible, $submit));
        $this->setAction(ADMIN_URL . '/pages/section/id/' . $options['id']);
        $t = Zend_Registry::get('Zend_Locale');
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма правки секции
     *
     * @param array $options Опции, пока только id = #ID секции
     * @return PageForm
     */
    public function EditSection($options = null) {
        $this->setName('section_settings');
        $t = Zend_Registry::get('Zend_Locale');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $model = new PageModel();
        $bid = new Zend_Form_Element_Select('bid');
        $bid->addMultiOptions($model->getBlocks($options['pid']))
                ->setLabel('Block')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $model = new SnippetModel();
        $sid = new Zend_Form_Element_Select('sid');
        $sid->addMultiOptions(array(0 => $t->_('Undefined')) + $model->getSnippetsForModule($options['mid']))
                ->setLabel('Snippet')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $visible = new Zend_Form_Element_Select('visible');
        $array = array(
            '0' => $t->_('Visible'),
            '2' => $t->_('Hidden'),
            '4' => $t->_('Closed')
        );

        $visible->addMultiOptions($array)
                ->setLabel('Visibility')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($name, $bid, $sid, $visible, $submit));
        $this->setAction(ADMIN_URL . '/pages/settings/id/' . $options['id']);
        $t = Zend_Registry::get('Zend_Locale');
        $this->setDefaultTranslator($t);
        return $this;
    }

}